package com.shijunhao.graduation.service.system.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shijunhao.graduation.domain.entity.SystemDictItem;
import com.shijunhao.graduation.domain.entity.SystemFileMapping;
import com.shijunhao.graduation.enums.DictCode;
import com.shijunhao.graduation.enums.TableName;
import com.shijunhao.graduation.mapper.SystemFileMappingMapper;
import com.shijunhao.graduation.service.system.ISystemDictItemService;
import com.shijunhao.graduation.service.system.ISystemDictService;
import com.shijunhao.graduation.service.system.ISystemFileMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shijunhao
 * @since 2022-03-09
 */
@Service
public class SystemFileMappingServiceImpl extends BaseServiceImpl<SystemFileMappingMapper, SystemFileMapping> implements ISystemFileMappingService {

    private final ISystemDictService systemDictService;

    @Autowired
    public SystemFileMappingServiceImpl(ISystemDictService systemDictService) {
        this.systemDictService = systemDictService;
    }

    @Override
    public boolean insert(TableName tableName, Long fileId, Long referenceId) {
        SystemFileMapping fileMapping = new SystemFileMapping();
        SystemDictItem dictItem = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), tableName.getLabel());
        fileMapping.setFileId(fileId);
        fileMapping.setReferenceId(referenceId);
        fileMapping.setTableDictValue(dictItem.getDictValue());
        return super.insertOne(fileMapping);
    }

    @Override
    public boolean delete(TableName tableName, Long fileId, Long referenceId) {
        SystemDictItem dictItem = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), tableName.getLabel());
        return super.deleteByCondition(Wrappers.lambdaUpdate(SystemFileMapping.class)
                .eq(SystemFileMapping::getFileId,fileId)
                .eq(SystemFileMapping::getReferenceId,referenceId)
                .eq(SystemFileMapping::getTableDictValue,dictItem.getDictValue()));
    }

    @Override
    public boolean delete(TableName tableName, Long referenceId) {
        SystemDictItem dictItem = systemDictService.loadByCodeLabel(DictCode.TABLE.getCode(), tableName.getLabel());
        return super.deleteByCondition(Wrappers.lambdaUpdate(SystemFileMapping.class)
                .eq(SystemFileMapping::getReferenceId,referenceId)
                .eq(SystemFileMapping::getTableDictValue,dictItem.getDictValue()));
    }
}
